<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录表单 -->
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginFroms"
        >
          <h1>hello</h1>
          <h2>欢迎来到登录界面</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              show-password
              type="password"
              v-model="loginForm.password"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              class="login_btn"
              type="primary"
              size="default"
              @click="Login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useUserStore } from '@/store/user/user'
const useStore = useUserStore()
import { useRouter } from 'vue-router'
const $router = useRouter()
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'

const loginForm = reactive({ username: 'admin', password: 'atguigu123' })
let loginFroms = ref()

// 登录按钮(这里使用pinia的User仓库定义userLogin方法发起请求)
const Login = async () => {
  // 表单验证通过才能执行下面逻辑（发请求）
  await loginFroms.value.validate()

  // 保证登陆成功
  try {
    //也可以使用.then/catch语法
    await useStore.userLogin(loginForm)
    $router.push('/')
    // console.log(123)

    // 登陆成功提示
    ElNotification({
      title: `HI,${getTime()}好`,
      type: 'success',
      message: '欢迎回来',
    })
  } catch (error) {
    ElNotification({
      type: 'error',
      message: '登陆失败,请检查账号或者密码是否正确',
    })
  }
}

// 表单验证逻辑

// const loginForm = reactive({username:'admin',password:'123456'})
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 5, max: 11, message: '账号长度不正确', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 11, message: '密码长度不正确', trigger: 'blur' },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover; //把背景图片放大到适合元素容器的尺寸，图片比例不变，但是要注意，超出容器的部分可能会裁掉

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png');
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
